package util;

import java.lang.reflect.Field;
import java.util.Date;
import java.util.EnumMap;
import java.util.List;

import org.apache.log4j.Logger;
import org.testng.Assert;
import org.testng.annotations.Test;

import uk.co.q3c.deplan.server.util.ReflectionFieldBuilder;
import uk.co.q3c.deplan.server.util.ReflectionFieldBuilder.FieldTypes;

/**
 * test for ReflectionFieldBuilder
 * 
 * @author DSowerby 28 Oct 2008
 * 
 */
@Test
public class ReflectionFieldBuilder_UT {
	transient protected final Logger logger = Logger.getLogger(getClass().getName());

	class TestObjectsub extends TestObject {
		BlankObject bob;
		FieldTypes ft;
	}

	class TestObject2 {
	}

	class BlankObject {
	}

	class TestObject3 {
		int integer;
		Date[] dates;
		Date date;
	}

	public void defaultExcludes() {
		ReflectionFieldBuilder rfb = new ReflectionFieldBuilder();
		EnumMap<FieldTypes, List<Field>> map = rfb.buildFieldList(new TestObjectsub());
		Assert.assertEquals(map.get(FieldTypes.ARRAY).size(), 1);
		Assert.assertEquals(map.get(FieldTypes.ENUM).size(), 1);
		Assert.assertEquals(map.get(FieldTypes.PRIMITIVE).size(), 8);
		Assert.assertEquals(map.get(FieldTypes.STRING).size(), 2);
		Assert.assertEquals(map.get(FieldTypes.COLLECTION).size(), 2);
		Assert.assertEquals(map.get(FieldTypes.MAP).size(), 1);

		Assert.assertEquals(map.get(FieldTypes.GENERALOBJECT).size(), 2);
	}

	public void excludeTransient() {
		ReflectionFieldBuilder rfb = new ReflectionFieldBuilder();
		rfb.setExcludeTransient(false);
		EnumMap<FieldTypes, List<Field>> map = rfb.buildFieldList(new TestObjectsub());
		Assert.assertEquals(map.get(FieldTypes.ARRAY).size(), 1);
		Assert.assertEquals(map.get(FieldTypes.ENUM).size(), 1);
		Assert.assertEquals(map.get(FieldTypes.PRIMITIVE).size(), 9);
		Assert.assertEquals(map.get(FieldTypes.STRING).size(), 2);
		Assert.assertEquals(map.get(FieldTypes.COLLECTION).size(), 2);
		Assert.assertEquals(map.get(FieldTypes.MAP).size(), 1);

		Assert.assertEquals(map.get(FieldTypes.GENERALOBJECT).size(), 2);
	}

	public void excludeNeither() {
		ReflectionFieldBuilder rfb = new ReflectionFieldBuilder();
		rfb.setExcludeTransient(false);
		rfb.setExcludeStatic(false);
		EnumMap<FieldTypes, List<Field>> map = rfb.buildFieldList(new TestObjectsub());
		Assert.assertEquals(map.get(FieldTypes.ARRAY).size(), 1);
		Assert.assertEquals(map.get(FieldTypes.ENUM).size(), 1);
		Assert.assertEquals(map.get(FieldTypes.PRIMITIVE).size(), 10);
		Assert.assertEquals(map.get(FieldTypes.STRING).size(), 2);
		Assert.assertEquals(map.get(FieldTypes.COLLECTION).size(), 2);
		Assert.assertEquals(map.get(FieldTypes.MAP).size(), 1);

		Assert.assertEquals(map.get(FieldTypes.GENERALOBJECT).size(), 2);
	}

}
